﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core.DatabaseHelpers;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Unity;
using Microsoft.Practices.Unity;

namespace Core.DAO
{
    class ActionDAO:AbstractDAO<IAction>,IActionDAO
    {
        public ActionDAO(IConnection connection) : base(connection)
        {
        }

        #region Overrides of AbstractDAO

       

       

        #endregion



        protected override void GetDependendModels(object domainobject, object[] row)
        {
            throw new NotImplementedException();
        }


        public bool DeleteAllLinks(IAction action)
        {
            Connection.ExecuteQuery("DELETE FROM building_action WHERE id_action=" + action.ActionId);
            return true;
        }

        public void SavePrice(IAction action)
        {
            foreach (var building in action.Buildings)
            {
                Connection.ExecuteQuery("UPDATE building_action SET startprice=" + building.StartPrice +
                                        " WHERE id_action=" + action.ActionId + " AND register_id='" +
                                        building.RegisterID + "'");
            }

        }

        public void GetPrices(IAction action)
        {
            var q = Connection.ExecuteSelectQuery("select * from building_action where id_action="+ action.ActionId, 3);
            foreach (object[] objects in q)
            {
                if (objects != null)
                {
                    var b = action.Buildings.First(x => x.RegisterID == objects[0].ToString());
                    b.StartPrice = int.Parse(objects[2].ToString());
                }
            }
        }
    }
}
